From d28f8bb07f2c76f55692be716e239ecf78842724 Mon Sep 17 00:00:00 2001 From: Jan Likar Date: Sat, 23 Jan 2016 22:16:30 +0100 Subject: [PATCH] Add field `publish` to Cargo.toml Field `publish` set to false can be used to prevent a package from being accidentally published. --- src/cargo/core/manifest.rs | 6 +++++- src/cargo/core/package.rs | 1 + src/cargo/ops/registry.rs | 5 +++++ src/cargo/util/toml.rs | 5 ++++- src/doc/manifest.md | 12 ++++++++++++ tests/test_cargo_publish.rs | 20 ++++++++++++++++++++ 6 files changed, 47 insertions(+), 2 deletions(-) diff --git a/src/cargo/core/manifest.rs b/src/cargo/core/manifest.rs index eece77a3b..5a4a48182 100644 --- a/src/cargo/core/manifest.rs +++ b/src/cargo/core/manifest.rs @@ -20,6 +20,7 @@ pub struct Manifest { include: Vec, metadata: ManifestMetadata, profiles: Profiles, + publish: bool } /// General metadata about a package which is just blindly uploaded to the @@ -163,7 +164,8 @@ impl Manifest { include: Vec, links: Option, metadata: ManifestMetadata, - profiles: Profiles) -> Manifest { + profiles: Profiles, + publish: bool) -> Manifest { Manifest { summary: summary, targets: targets, @@ -173,6 +175,7 @@ impl Manifest { links: links, metadata: metadata, profiles: profiles, + publish: publish, } } @@ -187,6 +190,7 @@ impl Manifest { pub fn version(&self) -> &Version { self.package_id().version() } pub fn warnings(&self) -> &[String] { &self.warnings } pub fn profiles(&self) -> &Profiles { &self.profiles } + pub fn publish(&self) -> bool { self.publish } pub fn links(&self) -> Option<&str> { self.links.as_ref().map(|s| &s[..]) } diff --git a/src/cargo/core/package.rs b/src/cargo/core/package.rs index 45242b076..9aa3c8669 100644 --- a/src/cargo/core/package.rs +++ b/src/cargo/core/package.rs @@ -79,6 +79,7 @@ impl Package { pub fn summary(&self) -> &Summary { self.manifest.summary() } pub fn targets(&self) -> &[Target] { self.manifest().targets() } pub fn version(&self) -> &Version { self.package_id().version() } + pub fn publish(&self) -> bool { self.manifest.publish() } pub fn has_custom_build(&self) -> bool { self.targets().iter().any(|t| t.is_custom_build()) diff --git a/src/cargo/ops/registry.rs b/src/cargo/ops/registry.rs index 3ae68e709..62b287959 100644 --- a/src/cargo/ops/registry.rs +++ b/src/cargo/ops/registry.rs @@ -34,6 +34,11 @@ pub fn publish(manifest_path: &Path, verify: bool) -> CargoResult<()> { let pkg = try!(Package::for_path(&manifest_path, config)); + if !pkg.publish() { + bail!("some crates cannot be published.\n\ + `{}` is marked as unpublishable", pkg.name()); + } + let (mut registry, reg_id) = try!(registry(config, token, index)); try!(verify_dependencies(&pkg, ®_id)); diff --git a/src/cargo/util/toml.rs b/src/cargo/util/toml.rs index 24da8c920..47b67aa0c 100644 --- a/src/cargo/util/toml.rs +++ b/src/cargo/util/toml.rs @@ -254,6 +254,7 @@ pub struct TomlProject { links: Option, exclude: Option>, include: Option>, + publish: Option, // package metadata description: Option, @@ -562,13 +563,15 @@ impl TomlManifest { keywords: project.keywords.clone().unwrap_or(Vec::new()), }; let profiles = build_profiles(&self.profile); + let publish = project.publish.unwrap_or(true); let mut manifest = Manifest::new(summary, targets, exclude, include, project.links.clone(), metadata, - profiles); + profiles, + publish); if project.license_file.is_some() && project.license.is_some() { manifest.add_warning(format!("warning: only one of `license` or \ `license-file` is necessary")); diff --git a/src/doc/manifest.md b/src/doc/manifest.md index 258ecf9f7..89e900f17 100644 --- a/src/doc/manifest.md +++ b/src/doc/manifest.md @@ -71,6 +71,18 @@ necessary source files may not be included. [globs]: http://doc.rust-lang.org/glob/glob/struct.Pattern.html + +## The `publish` Field (optional) + +The `publish` field can be used to prevent a package from being +published to a repository by mistake. + +```toml +[package] +# ... +publish = false +``` + ## Package metadata There are a number of optional metadata fields also accepted under the diff --git a/tests/test_cargo_publish.rs b/tests/test_cargo_publish.rs index a30a63f8d..c985504b6 100644 --- a/tests/test_cargo_publish.rs +++ b/tests/test_cargo_publish.rs @@ -137,3 +137,23 @@ all path dependencies must have a version specified when publishing. dependency `bar` does not specify a version ")); }); + +test!(unpublishable_crate { + let p = project("foo") + .file("Cargo.toml", r#" + [project] + name = "foo" + version = "0.0.1" + authors = [] + license = "MIT" + description = "foo" + publish = false + "#) + .file("src/main.rs", "fn main() {}"); + + assert_that(p.cargo_process("publish"), + execs().with_status(101).with_stderr("\ +some crates cannot be published. +`foo` is marked as unpublishable +")); +}); -- 2.30.2